#coding=utf-8
"""
@version=1.0
@author:zsh
@file:DateModel.py
@time:2019/1/11 20:00
"""
from functools import reduce

import math
class myDate():
    months = [31, 31, 28, 31 , 30, 31, 30, 31, 31, 30, 31, 30, 31,31]
    def __init__(self,year,month,day):
        self.year = year
        self.month = month
        self.day = day
        self.flag = True
        myDate.months[2] = 29 if self.isLeapYear() else 28
        if not (isinstance(self.day,(int,float)) and isinstance(self.month,(int,float)) and isinstance(self.year,(int,float))):
            self.flag = False
        if self.month<=0 or self.month>12:
            self.flag = False
        if self.day > myDate.months[self.month] or self.day<=0:
            self.flag = False
        if self.year==0:
            self.flag=False



    def isLeapYear(self):
        return ((not self.year%4 and self.year%100) or not self.year%400) and self.year>0

    def __add__(self, delta):
        if not self.flag or not isinstance(delta,(int,float)) or math.isnan(delta):
            return 'nan'
        delta=int(delta)
        myDate.months[2] = 29 if self.isLeapYear() else 28
        while delta>myDate.months[self.month]:
            print(self,myDate.months[self.month],delta)
            delta-=myDate.months[self.month]
            self.month+=1
            if self.month==13:
                self.month=1
                self.year+=1
                if self.year==0:
                    self.year=1
                myDate.months[2] = 29 if self.isLeapYear() else 28
        print(self, myDate.months[self.month], delta)
        if delta+self.day>myDate.months[self.month]:
            delta-=myDate.months[self.month]-self.day
            self.month += 1
            if self.month == 13:
                self.month = 1
                self.year += 1
                if self.year==0:
                    self.year=1
                myDate.months[2] = 29 if self.isLeapYear() else 28
            self.day=delta if delta>0 else 1
        else:
            self.day+=delta
        return self
    def __sub__(self, delta):
        if not self.flag or not isinstance(delta,(int,float)) or math.isnan(delta):
            return 'nan'
        delta=int(delta)
        myDate.months[2] = 29 if self.isLeapYear() else 28
        while delta>=myDate.months[self.month]:
            delta-=myDate.months[self.month-1]
            self.month-=1
            if self.month==0:
                self.month=12
                self.year-=1
                if self.year==0:
                    self.year=-1
                myDate.months[2] = 29 if self.isLeapYear() else 28
        if delta>=self.day:
            delta-=self.day
            self.month-=1
            if self.month==0:
                self.month=12
                self.year-=1
                if self.year==0:
                    self.year=-1
                myDate.months[2] = 29 if self.isLeapYear() else 28
            if delta>=0:
                self.day=myDate.months[self.month]-delta
            else:
                self.day=myDate.months[self.month]-1
        else:
            self.day-=delta
        return self

    def __str__(self):
        return '{}/{}/{}'.format(self.year,self.month,self.day)

    def __eq__(self, other):
        return isinstance(other,myDate) and other.year==self.year and other.month==self.month and other.day ==self.day
if __name__ == '__main__':
    res=myDate(2017,12,3)-4
    # res1 = myDate(15,4,3)-24
    print(res)
    print(isinstance(float('nan'),float))